*new variable prep*
egen c_arrestextradite = rowmax(c_usarrest c_extradition)
gen nc_arrestextradite =  nc_extradition 
replace nc_arrestextradite = 0 if c_arrest == 1
egen nc_messagingx = rowmax(nc_messaging nc_downplay)
replace nc_messagingx = 0 if c_messaging == 1
egen nc_intlforumx = rowmax(nc_noncomply nc_intlforum)
egen c_usmil = rowmax(c_supportus c_participateus)
egen nc_usmil = rowmax(nc_opposeus nc_protestactions) 
replace nc_usmil = 0 if c_usmil == 1

*new CTCS*
egen countX = rowtotal(c_ctacts c_financing c_bordersandimmigration c_legalchange c_institchange c_intelshare c_allowus c_hostus c_overflight c_messaging c_arrestextradite c_usmil nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_intelshare nc_messagingx nc_usmil)
egen CcountX = rowtotal(c_ctacts c_financing c_bordersandimmigration c_legalchange c_institchange c_intelshare c_allowus c_hostus c_overflight c_messaging c_arrestextradite c_usmil)
egen NCcountX = rowtotal(nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_intelshare nc_messagingx nc_usmil)
gen percentcoopX = CcountX/countX
gen percentnoncoopX = NCcountX/countX
gen percentcoopdiffX = percentcoopX - percentnoncoopX
gen CTCSx_count = CcountX - NCcountX
gen CTCSx_oldversion = percentcoopdiffX
replace CTCSx_oldversion = 0 if percentcoopdiffX == .
gen CTCS_coop = percentcoopX
replace CTCS_coop = 0 if percentcoopX == .
gen CTCS_noncoop = percentnoncoopX
replace CTCS_noncoop = 0 if percentnoncoopX == .

**just paired**
egen countX_p = rowtotal(c_ctacts c_financing c_bordersandimmigration c_legalchange c_intelshare c_messaging c_arrestextradite c_intlforum c_usmil nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_bordersandimmigration nc_intelshare nc_messagingx nc_intlforumx nc_usmil)
egen CcountX_p = rowtotal(c_ctacts c_financing c_bordersandimmigration c_legalchange c_intelshare c_messaging c_arrestextradite c_intlforum c_usmil)
egen NCcountX_p = rowtotal(nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_bordersandimmigration nc_intelshare nc_messagingx nc_intlforumx nc_usmil)
gen CTCSx_paired = (CcountX_p/countX_p)/(NCcountX_p - countX_p)

**dichot and ordinal**
gen CTCSx_dichot = 0
replace CTCSx_dichot= 1 if CTCS < 0

gen CTCSx_ord = 0
replace CTCSx_ord = 2 if CTCS > 0
replace CTCSx_ord = 1 if CTCS < 0

*factor*
alpha c_ctacts c_financing c_bordersandimmigration c_legalchange c_institchange c_intelshare c_allowus c_hostus c_overflight c_messaging c_arrestextradite c_usmil nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_intelshare nc_messagingx nc_usmil

tetrachoric c_ctacts c_financing c_bordersandimmigration c_legalchange c_institchange c_intelshare c_allowus c_hostus c_overflight c_messaging c_arrestextradite c_usmil, ze posdef
matrix C = r(Rho)
factormat C, n(674) factors(2)
rotate, varimax
predict TetraCoopfactor1 TetraCoopfactor2

pcamat C, n(674) components(1)
rotate, varimax
predict CoopPCA

tetrachoric nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_intelshare nc_messagingx nc_usmil, ze posdef
matrix NC = r(Rho)
factormat NC, n(674) factors(2)
rotate, varimax
predict TetraNoncoopfactor1 TetraNoncoopfactor2

pcamat NC, n(674) components(1)
rotate, varimax
predict NCoopPCA

egen maxNCPCA = max(NCoopPCA)
egen maxCPCA = max(CoopPCA)
gen NCPCA_s = (NCoopPCA/maxNCPCA )* 10
gen CPCA_s = (CoopPCA/maxCPCA) * 10
gen PCAdiff = CPCA_s - NCPCA_s

*alternate measure-ordinal*

gen ctacts_o = 0
replace ctacts_o = 1 if c_ctacts == 1
replace ctacts_o = -1 if nc_ctacts == 1

gen financing_o = 0
replace financing_o = 1 if c_financing == 1
replace financing_o = -1 if nc_financing == 1

gen borders_o = 0
replace borders_o = 1 if c_bordersandimmigration == 1
replace borders_o = -1 if nc_bordersandimmigration == 1

gen legal_o = 0
replace legal_o = 1 if c_legalchange == 1
replace legal_o = -1 if nc_legalchange == 1

gen intel_o = 0
replace intel_o = 1 if c_intelshare == 1
replace intel_o = -1 if nc_intelshare == 1

egen milhelp = rowmax(c_allowus c_hostus c_overflight)
replace milhelp = -1 if milhelp == 0

gen messaging_o = 0
replace messaging_o = 1 if c_messaging == 1
replace messaging_o = -1 if nc_messagingx == 1

gen arrest_o = 0
replace arrest_o = 1 if c_arrest == 1
replace arrest_o = -1 if nc_arrest == 1

gen usmil_o = 0
replace usmil_o = 1 if c_usmil == 1
replace usmil_o = -1 if nc_usmil == 1

egen CTCS_o_avg = rowmean( ctacts_o financing_o borders_o legal_o intel_o milhelp messaging_o arrest_o usmil_o) 

*alternate measure-Polity*
gen CcountX_scaled = CcountX/8 * 10
gen NCcountX_scaled = NCcountX/5 * 10
gen CTCS_sum = CcountX_scaled - NCcountX_s

egen maxCcountX_p = max(CcountX_p)
egen maxNCcountX_p = max(NCcountX_p)
gen CcountX_p_s = (CcountX_p/maxCcountX_p) * 10
gen NCcountX_p_s = (NCcountX_p/maxNCcountX_p) * 10
gen CTCS_sum_p = CcountX_p_s - NCcountX_p_s



egen CavgX = rowmean(c_ctacts c_financing c_bordersandimmigration c_legalchange c_institchange c_intelshare c_allowus c_hostus c_overflight c_messaging c_arrestextradite c_usmil)
egen NCavgX = rowmean(nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_intelshare nc_messagingx nc_usmil)
egen maxCavgX = max(CavgX)
egen maxNCavgX = max(NCavgX)
gen Cavg_s = (CavgX/maxCavgX)*10
gen NCavg_s = (NCavgX/maxNCavgX)*10
gen CTCS = Cavg_s - NCavg_s

egen CavgX_p = rowmean(c_ctacts c_financing c_bordersandimmigration c_legalchange c_intelshare c_messaging c_arrestextradite c_intlforum c_usmil)
egen NCavgX_p = rowmean(nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_bordersandimmigration nc_intelshare nc_messagingx nc_intlforumx nc_usmil)
egen maxCavgX_p = max(CavgX_p)
egen maxNCavgX_p = max(NCavgX_p)
gen Cavg_s_p = (CavgX_p/maxCavgX_p)*10
gen NCavg_s_p = (NCavgX_p/maxNCavgX_p)*10
gen CTCS_p = Cavg_s_p - NCavg_s_p

gen CTCS_nomiss = CTCS
replace CTCS_nomiss = . if CavgX == 0 & NCavgX == 0
*subs**

egen CIntl = rowmean(c_intelshare c_allowus c_hostus c_overflight c_usmil)
egen NCIntl = rowmean(nc_intelshare nc_usmil)
egen maxCIntl = max(CIntl)
egen maxNCIntl = max(NCInt)
gen CIntl_s = (CIntl/maxCIntl)*10
gen NCIntl_s = (NCIntl/maxNCIntl)*10
gen Intl_avg = CIntl_s - NCIntl_s

egen CDom = rowmean(c_ctacts c_financing c_bordersandimmigration c_legalchange c_institchange c_messaging c_arrestextradite)
egen NCDom = rowmean(nc_ctacts nc_arrestextradite nc_financing nc_legalchange nc_messagingx)
egen maxDom = max(CDom)
egen maxNCDom = max(NCDom)
gen CDom_s = (CDom/maxDom)*10
gen NCDom_s = (NCDom/maxNCDom)*10
gen Dom_avg = CDom_s - NCDom_s

*separating out domestics*
egen Cacts = rowmean(c_ctacts c_messaging c_arrestextradite)
egen NCacts = rowmean(nc_ctacts nc_arrestextradite nc_messagingx)
egen maxCacts = max(Cacts)
egen maxNCacts = max(NCacts)
gen Cacts_avg = (Cacts/maxCacts)*10
gen NCacts_avg = (NCacts/maxNCacts)*10
gen CTCS_acts = Cacts_avg - NCacts_avg

egen CUN = rowmean(c_financing c_bordersandimmigration c_legalchange c_institchange)
egen NCUN = rowmean(nc_financing nc_legalchange)
egen maxCUN = max(CUN)
egen maxNCUN = max(NCUN)
gen CUN_avg = (CUN/maxCUN)*10
gen NCUN_avg = (NCUN/maxNCUN)*10
gen CTCS_UN = CUN_avg - NCUN_avg
